<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 后台基类
 * @property Admin_model $admin_model
 */
class Admin_Controller extends CI_Controller {


    public     $userid    = null,
                $username  = null,
                $roleid    = null,
                $departmentid = null;

    public function __construct() {
        parent::__construct();

        $this->load->helper(array("common", "url"));
        //验证用户是否登陆

        if($this->router->fetch_class() != 'admins'){
            $flag = $this->check_login();
            if(empty($flag)){
                cpmessage('请重新登陆', '/index.php?c=admins');
            }
        }
    }

    /**
     *   验证登陆
     */

    function check_login()
    {
        $admin = isset($_COOKIE['username']) ? $_COOKIE['username'] : null;
        return $admin;
    }

    public function _before_execute(){
        $str = isset($_COOKIE['admin_auth']) ? $_COOKIE['admin_auth'] : false;
        if ( $str )
        {
            list( $userid, $username, $roleid, $departmentid ) = explode( "\t", str_decode( $str, $this->config->item( "authkey" ) ) );
            if ( is_numeric( $userid ) )
            {
                $this->userid   = $userid;
                $this->username = $username;
                $this->roleid   = $roleid;
                $this->departmentid = $departmentid;
            }
        }
        else
        {
            if ( !in_array( $this->router->class."/".$this->router->method, array( "admins/login", "admins/logout" ) ) )
            {
                header( "location:/admins/" );
                exit( );
            }
        }
    }

    public static function is_get()
    {
        return $_SERVER['REQUEST_METHOD'] == 'GET';
    }

    public static function is_post()
    {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }

    public static function is_ajax()
    {
        return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }

}




class Base_Controller extends CI_Controller {

    public     $userid    = null,
                $username  = null;

    public function __construct() {
        parent::__construct();
        $this->load->helper(array("common", "url", "file"));
        //$this->_before_execute();
    }

    public function _before_execute(){
        $str = isset($_COOKIE['auth']) ? $_COOKIE['auth'] : false;
        if ( $str )
        {
            list( $userid, $username ) = explode( "\t", str_decode( $str, $this->config->item( "authkey" ) ) );
            if ( is_numeric( $userid ) )
            {
                $this->userid = $userid;
                $this->username = $username;
            }
        }
        else
        {
            if ( !in_array( $this->router->class."/".$this->router->method, array( "member/login", "member/logout" ) ) )
            {
                header( "location:/member/login/" );
                exit( );
            }
        }
    }

    public static function is_get()
    {
        return $_SERVER['REQUEST_METHOD'] == 'GET';
    }

    public static function is_post()
    {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }

    public static function is_ajax()
    {
        return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    }



}



//End Admin Controller class
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */